﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ls.ImageSharep.App
{
    public partial class UCTemplate : UserControl
    {
        public UCTemplate()
        {
            InitializeComponent();
        }

        public CallTemplateBack templateCallBack;
        private int selectIndex = 0;
        private void UCTemplate_Load(object sender, EventArgs e)
        {
            if (frmImagMain.Instance.TemplateStream != null)
            {
                pbtemplate.Image = System.Drawing.Image.FromStream(BytesToStream(frmImagMain.Instance.TemplateStream));
            }
            else
            {
                pbtemplate.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/invoice.png");
            }
           
            this.pbtemplate.SizeMode = PictureBoxSizeMode.AutoSize;

            lbitems.DataSource = LsApp.ListItems;
            lbitems.DisplayMember = "DisplayText";
            lbitems.ValueMember = "Id";

        }

        private void pbtemplate_Click(object sender, EventArgs e)
        {
            //Point ms = Control.MousePosition; //获取鼠标位置
            Point ms = pbtemplate.PointToClient(Control.MousePosition);
            //lbitems.Items.Add($"{ms.X}:{ms.Y}||默认内容");
            LsApp.ListItems.Add(new ListDataItemModel { X = ms.X, Y = ms.Y, Content = "默认内容", DisplayText=$"x-y;{ms.X}:{ms.Y}",Id = LsApp.ListItems.Count()+1,WrappingLength =0,FontSize=20 });
            lbitems.DataSource = null;
            lbitems.DataSource = LsApp.ListItems;
            lbitems.DisplayMember = "DisplayText";
            lbitems.ValueMember = "Id";
        }
        private void btnok_Click(object sender, EventArgs e)
        {
            try
            {
                if (selectIndex > 0)
                {
                    var data = LsApp.ListItems.Where(w => w.Id == selectIndex).First();
                    data.X = Convert.ToInt32(txtx.Text);
                    data.Y = Convert.ToInt32(txty.Text);
                    data.Content = txtcontent.Text.ToString();
                    data.DisplayText = $"x-y;{data.X}:{data.Y}";
                    data.WrappingLength = Convert.ToInt32(txtWarpLength.Text);
                    data.FontSize = Convert.ToSingle(txtfontsize.Text);

                    lbitems.DataSource = null;
                    lbitems.DataSource = LsApp.ListItems;
                    lbitems.DisplayMember = "DisplayText";
                    lbitems.ValueMember = "Id";

                    txtx.Text = String.Empty;
                    txty.Text = String.Empty;
                    txtcontent.Text = String.Empty;
                    txtWarpLength.Text = String.Empty;
                    txtfontsize.Text = String.Empty;
                }
                selectIndex = 0;
            }
            catch (Exception ex)
            {


            }

        }
        private void btndel_Click(object sender, EventArgs e)
        {
            try
            {
                var data = LsApp.ListItems.Where(w => w.Id == selectIndex).First();
               
                LsApp.ListItems.Remove(data);
                lbitems.DataSource = null;
                lbitems.DataSource = LsApp.ListItems;
                lbitems.DisplayMember = "DisplayText";
                lbitems.ValueMember = "Id";
            }
            catch (Exception ex)
            {

           
            }
        }
        private void btnxzmb_Click(object sender, EventArgs e)
        {
            frmImagMain.Instance.PnlContainer.Controls["UCImage"].BringToFront();
            if (templateCallBack != null)
            {
                templateCallBack();
            }
            frmImagMain.Instance.BackButton.Visible = false;
        }

        private void lbitems_SelectedIndexChanged(object sender, EventArgs e)
        {

            var data = lbitems.SelectedItem as ListDataItemModel;
            if (data != null)
            {
                selectIndex = data.Id;
                txtx.Text = data.X.ToString();
                txty.Text = data.Y.ToString();
                txtcontent.Text = data.Content.ToString();
                txtWarpLength.Text = data.WrappingLength.ToString();
                txtfontsize.Text = data.FontSize.ToString();

            }
        }


        #region 私有方法

        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// 将 byte[] 转成 Stream

        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
        #endregion

       
    }
}
